/**
 * 通用混合
 * */
import { cloneDeep } from "lodash";
export default {
    data() {
        return {
            funaskData: "",
            funupdate: "",
            funcreate: "",
            fundelete: "",
            grid: { xl: 4, lg: 8, md: 12, sm: 24, xs: 24 }, // 栅格布局：xl超大、lg台式、md笔记本、sm平板、xs手机
            loading: false,
            edit: true, // 新增true,编辑false
            showedit: false, // 新增编辑模态框
            total: 0,
            subloading: false,
            pageOpts: [10, 15, 20, 30, 50], // 分页每页数量
            initialpage: 1,
            initialpageSize: 20,
            modalparamsinitial: {id:''}
        };
    },
    mounted() {
        this.initial();
    },
    methods: {
        askData() {
            this.loading = true;
            this.funaskData(this.params).then((res) => {
                this.loading = false;
                this.askDatas(res);
            });
        },
        askDatasearch() {
            this.searchparams();
            this.params.page = 1;
            this.askData();
        },
        askDatachange() {
            this.params.page = 1;
            this.askData();
        },
        /* @函数功能: 分页 */
        changepage(e) {
            this.params.page = e;
            this.askData();
        },
        /* @函数功能: 每页数量 */
        changepageSize(e) {
            this.params.pageSize = e;
            this.askData();
        },
        /* @函数功能: 打开编辑或新增 */

        openedit(row) {
            if (row === -1) {
                this.edit = true;
                this.modalparamscancel();
            } else {
                this.edit = false;
                if (this.$refs.formItem) this.$refs.formItem.resetFields();
                this.paramsedit(row);
            }
            this.showedit = true;
        },
        /* @函数功能: 取消编辑或新增 */

        canceledit(name) {
            if (name) this.$refs[name].resetFields();
            if (this.eChartPage) {
                this.eChartPage = 1;
            }
            this.showedit = false;
            this.modalparamscancel();
        },
        /* @函数功能: 提交编辑或新增 */

        submitedit(name) {
            if (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.submitedits(name);
                    }
                });
            } else {
                this.submitedits(name);
            }
        },
        submitedits(name) {
            let config = this.paramssubmit();
            if (config) {
                this.subloading = true;
                if (!this.edit) {
                    this.funupdate(config)
                        .then(() => {
                            this.beforsubmit(name);
                            this.subloading = false;
                        })
                        .catch(() => {
                            this.subloading = false;
                        });
                } else {
                    this.funcreate(config)
                        .then(() => {
                            this.beforsubmit(name);
                            this.subloading = false;
                        })
                        .catch(() => {
                            this.subloading = false;
                        });
                }
            }
        },
        /* @函数功能: 提交之后数据处理 */
        beforsubmit(name) {
            if (name) this.$refs[name].resetFields();
            if (!this.edit) this.$Message.success("编辑成功");
            else this.$Message.success("新增成功");
            this.askData();
            this.$emit("askData");
            this.modalparamscancel();
            this.showedit = false;
        },
        /* @函数功能: 参数置为初始值 */
        modalparamscancel(){
            this.modalparams = cloneDeep(this.modalparamsinitial)
        },
        /* @函数功能: 删除数据 */
        deleteedit(params, text) {
            this.$Modal.confirm({
                title: text.title,
                content: `<p>${text.content}</p>`,
                onOk: () => {
                    this.fundelete(params).then(() => {
                        this.$Message.info(text.success || "删除成功");
                        this.askData();
                    });
                },
            });
        },
    },
};
